function WindowManager() {
	this.windows = [];
	this.zIndex = 0;
}
WindowManager.prototype.find = function (id) {
	for (var i = 0; i < this.windows.length; i ++)	{
		if (this.windows[i].id == id) {
			return this.windows[i];
		} 
	}
	return null;
};
WindowManager.prototype.remove = function (id) {
	for (var i = 0; i < this.windows.length; i ++)	{
		if (this.windows[i].id == id) {
			if (this.windows[i].winObj.style.zIndex == this.zIndex) this.zIndex--;
			this.windows.removeAt(i);
		}
	}
};
WindowManager.prototype.append = function (obj) {
	this.windows[this.windows.length] = obj;
};
WindowManager.prototype.showAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].show();
	}
};
WindowManager.prototype.hideAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].hide();
	}
};
WindowManager.prototype.inactiveAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].inActive();
	}
};
WindowManager.prototype.calLeft = function () {
	if (this.windows.length != 0)
		return this.windows[this.windows.length - 1].left + 10;
	else
		return 200;
}
WindowManager.prototype.calTop = function () {
	if (this.windows.length != 0)
		return this.windows[this.windows.length - 1].top + 10;
	else
		return 200;
}

WindowManager.prototype.closeAll = function () {
	for (var i = this.windows.length - 1; i >= 0; i --)	{
		this.windows[i].close();
	}
}

WindowManager.prototype.minimizeAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].doMinimize();
	}
}
WindowManager.prototype.restoreAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].doRestore();
	}
}
WindowManager.prototype.cascadeAll = function () {
	for (var i = 0; i < this.windows.length; i ++)	{
		this.windows[i].doMinimize();
	}
}

WindowManager.prototype.cascadeAll = function () {
	this.restoreAll();
	
	for (var i = this.windows.length - 1, nowLeft = 100, nowTop = 100; i >=0 ; i--, nowLeft+=10, nowTop+=10)	{
		this.windows[i].locate(nowLeft, nowTop);
		this.windows[i].resize(600, 400);
		this.windows[i].doActive();
	}
}
WindowManager.prototype.horizontalAll = function () {
	this.restoreAll();
	
	var l = parseInt(this.windows.length/4)+(this.windows.length%4==0?0:1);
	var width = parseInt(document.body.clientWidth - 10)/4;
	var height = parseInt(document.body.clientHeight - 60)/l;
	var firstTop = 5;
	var firstLeft = 5;
	
	for (var i = 0, left = firstLeft, top = firstTop; i < this.windows.length; i ++)	{
		this.windows[i].locate(left, top);
		this.windows[i].resize(width, height);
		this.windows[i].doActive();
		
		left += width;
		if (left >= firstLeft+width*4) {
			left = firstLeft;
			top += height;
		}
	}
}
WindowManager.prototype.verticalAll = function () {
	this.restoreAll();
	
	var l = parseInt(this.windows.length/4)+(this.windows.length%4==0?0:1);
	var width = parseInt(document.body.clientWidth - 10)/l;
	var height = parseInt(document.body.clientHeight - 60)/4;
	var firstTop = 5;
	var firstLeft = 5;
	
	for (var i = 0, left = firstLeft, top = firstTop; i < this.windows.length; i ++)	{
		this.windows[i].locate(left, top);
		this.windows[i].resize(width, height);
		this.windows[i].doActive();
		
		top += height;
		if (top >= firstTop+height*4) {
			top = firstTop;
			left += width;
		}
	}
}



function TaskBar(divId, width) {
	WindowManager.call(this);
	
	this.taskObj = document.getElementById(divId);
	this.width = (width)?width:800;//默认800宽
	this.taskObj.style.width = this.width;
	//仅仅用于控制窗口最大化不会覆盖taskbar。
	this.height = 0;
	
	this.groups = [];
	
	var oThis = this;
 	//初始化右键菜单事件
 	ContextMenu.intializeContextMenu();
	
	//右键事件
	this.taskObj.oncontextmenu = function () {
		var popupoptions = [
   		new ContextItem("层叠全部", function() { oThis.cascadeAll(); }, false),
			new ContextItem("横向平铺全部", function() { oThis.horizontalAll(); }, false),
			new ContextItem("纵向平铺全部", function() { oThis.verticalAll(); }, false),
			new ContextSeperator(),
			new ContextItem("最小化全部", function() { oThis.minimizeAll(); }, false),
			new ContextItem("还原全部", function() { oThis.restoreAll(); }, false),
			new ContextSeperator(),
			new ContextItem("关闭全部", function() { oThis.closeAll(); }, false)
		];
		ContextMenu.display(popupoptions);
	}
}
TaskBar.prototype = new WindowManager;

TaskBar.prototype.createTaskGroup = function (gid, name, icon) {
	//find old
	for (var i = 0; i < this.groups.length; i ++) {
		if (this.groups[i].gid == gid) {
			return this.groups[i];
		}
	}
	
	//create new
	var ng = new TaskGroup(this, gid, name, icon);
	
	//add group
	this.taskObj.appendChild(ng.groupObj);
	this.groups[this.groups.length] = ng;
	
	this.calculate();
	return ng;
}

TaskBar.prototype.removeTaskGroup = function (gid) {
	for (var i = 0; i < this.groups.length; i ++) {
		if (this.groups[i].gid == gid) {
			//remove group
			this.taskObj.removeChild(this.groups[i].groupObj);
			this.groups.removeAt(i);
			break;
		}
	}
	this.calculate();
}

TaskBar.prototype.removeTaskButton = function (gid, id) {
	for (var i = 0; i < this.groups.length; i ++) {
		if (this.groups[i].gid == gid) {
			//remove button
			this.groups[i].removeTaskButton(id);
			
			//remove group
			if (this.groups[i].buttons.length == 0)
				this.removeTaskGroup(gid);
			break;
		}
	}
}

TaskBar.prototype.calculate = function () {
	var gl = this.groups.length;
	var bw = this.width;
	var gw = 150;//默认100宽,根据窗口组的个数进行调节，调节时注意对窗口组名称的调整
	if (gl == 0) return;
	
	//cal width
	if (gl*(gw+1) + 1 > bw) {
		gw = (bw/gl) - 2;
	}
	
	//cal pos
	for (var i = 0; i < this.groups.length; i ++) {
		this.groups[i].setPosition((1*(i+1))+(i*gw), gw);
	}
}

TaskBar.prototype.clear = function () {
	for (var i = 0; i < this.groups.length; i ++) {
		this.groups[i].selected = false;
		this.groups[i].groupObj.className = "taskbutton_div";
	}
}

TaskBar.prototype.hide = function () {
	for (var i = 0; i < this.groups.length; i ++) {
		this.groups[i].menuObj.style.display = "none";
	}
}


function TaskGroup(bar, gid, name, icon) {
	this.buttons = [];
	this.bar = bar;
	
	this.gid = gid;
	this.name = name;
	this.icon = icon;
	this.selected = false;
	
	
	//create task group div obj
	//property
	this.groupObj = document.createElement("div");
	this.groupObj.id = this.gid;
	this.groupObj.className = "taskbutton_div";
	this.groupObj.innerHTML = this.name;
	
	
	//event	
	//click
	var oThis = this;
	this.groupObj.onclick = function () {
		oThis.bar.clear();
		oThis.selected = true;
		oThis.groupObj.className = "taskbutton_div_selected";
		
		if (oThis.buttons.length != 0) {
			if (oThis.menuObj.style.display == 'none')
				oThis.showTaskMenu();
			else
				oThis.hideTaskMenu();
		}
	}
	
	//右键事件
	this.groupObj.oncontextmenu = function () {
		var popupoptions = [
   			new ContextItem("层叠", function() { oThis.cascadeGroup() }, false),
			new ContextItem("横向平铺", function() { oThis.verticalGroup(); }, false),
			new ContextItem("纵向平铺", function() { oThis.horizontalGroup(); }, false),
			new ContextSeperator(),
			new ContextItem("最小化组", function() { oThis.minimizeGroup(); }, false),
			new ContextItem("还原组", function() { oThis.restoreGroup(); }, false),
			new ContextSeperator(),
			new ContextItem("关闭组", function() {  oThis.closeGroup();  }, false)
		];
		ContextMenu.display(popupoptions);
	}
	
	//hover
	this.groupObj.onmouseover = function () {
		if (!oThis.selected)
			oThis.groupObj.className = "taskbutton_div_hover";
		oThis.bar.hide();
	}
	this.groupObj.onmouseout = function () {
		if (!oThis.selected)
			oThis.groupObj.className = "taskbutton_div";
	}
		
	//create task menu div obj
	this.menuObj = document.getElementById(this.gid + ".menu");
	this.menuObj = (this.menuObj)?this.menuObj:document.createElement("div");
	this.menuObj.id = this.gid + ".menu";
	this.menuObj.className = "taskmenu_div"
	this.menuObj.style.display = "none";
	this.menuObj.style.width = 200;//默认200宽，根据窗口名称进行调节
	
	//add menu
	this.bar.taskObj.insertAdjacentElement("afterEnd", this.menuObj);
	//this.menuObj.onmouseleave = function () {
		//oThis.hideTaskMenu();
	//}
	
	
}
TaskGroup.prototype.setPosition = function (l, w) {
	this.groupObj.style.left = l;
	this.groupObj.style.width = w;
}

TaskGroup.prototype.createTaskButton = function (id, name, icon) {
	//find old
	for (var i = 0; i < this.buttons.length; i ++) {
		if (this.buttons[i].id == id) {
			return this.buttons[i];
		}
	}
	
	//create
	var nb = new TaskButton(this, id, name, icon);
	
	//add button
	this.menuObj.appendChild(nb.buttonObj);
	this.buttons[this.buttons.length] = nb;
	
	return nb;
}
TaskGroup.prototype.removeTaskButton = function (id) {
	for (var i = 0; i < this.buttons.length; i ++) {
		if (this.buttons[i].id == id) {
			//remove button
			this.menuObj.removeChild(this.buttons[i].buttonObj);
			this.buttons.removeAt(i);
			break;
		}
	}
}
TaskGroup.prototype.showTaskMenu = function () {
	this.menuObj.style.zIndex = ++this.bar.zIndex;
	
	this.menuObj.style.left = posLib.getBoundingLeft(this.groupObj);
	this.menuObj.style.top = posLib.getBoundingTop(this.bar.taskObj) - this.getTaskMenuHeight();
	
	this.menuObj.style.display = "block";
	
}
TaskGroup.prototype.getTaskMenuHeight = function () {
	return 20*this.buttons.length + 6;//这里的20和6，一旦css中的相应数值修改，这里也要修改
}
TaskGroup.prototype.hideTaskMenu = function () {
	this.menuObj.style.display = "none";
}
TaskGroup.prototype.closeGroup = function () {
	for (var i = this.buttons.length - 1; i >= 0 ; i --) {
		this.buttons[i].window.close();
	}
}
TaskGroup.prototype.minimizeGroup = function () {
	for (var i = 0; i < this.buttons.length; i ++) {
		this.buttons[i].window.doMinimize();
	}
}
TaskGroup.prototype.restoreGroup = function () {
	for (var i = 0; i < this.buttons.length; i ++) {
		this.buttons[i].window.doRestore();
	}
}
TaskGroup.prototype.cascadeGroup = function () {
	this.bar.minimizeAll();
	this.restoreGroup();
	
	for (var i = this.buttons.length - 1, nowLeft = 100, nowTop = 100; i >=0 ; i--, nowLeft+=10, nowTop+=10)	{
		this.buttons[i].window.locate(nowLeft, nowTop);
		this.buttons[i].window.resize(600, 400);
		this.buttons[i].window.doActive();
	}
}
TaskGroup.prototype.horizontalGroup = function () {
	this.bar.minimizeAll();
	this.restoreGroup();
	
	var l = parseInt(this.buttons.length/4)+(this.buttons.length%4==0?0:1);
	var width = parseInt(document.body.clientWidth - 10)/4;
	var height = parseInt(document.body.clientHeight - 60)/l;
	var firstTop = 5;
	var firstLeft = 5;
	
	for (var i = 0, left = firstLeft, top = firstTop; i < this.buttons.length; i ++)	{
		this.buttons[i].window.locate(left, top);
		this.buttons[i].window.resize(width, height);
		this.buttons[i].window.doActive();
		
		left += width;
		if (left >= firstLeft+width*4) {
			left = firstLeft;
			top += height;
		}
	}
	
}
TaskGroup.prototype.verticalGroup = function () {
	this.bar.minimizeAll();
	this.restoreGroup();
	
	var l = parseInt(this.buttons.length/4)+(this.buttons.length%4==0?0:1);
	var width = parseInt(document.body.clientWidth - 10)/l;
	var height = parseInt(document.body.clientHeight - 60)/4;
	var firstTop = 5;
	var firstLeft = 5;
	
	for (var i = 0, left = firstLeft, top = firstTop; i < this.buttons.length; i ++)	{
		this.buttons[i].window.locate(left, top);
		this.buttons[i].window.resize(width, height);
		this.buttons[i].window.doActive();
		
		top += height;
		if (top >= firstTop+height*4) {
			top = firstTop;
			left += width;
		}
	}
}


function TaskButton(group, id, name, icon) {
	this.group = group;
	this.window = null;
	
	this.id = id;
	this.name = name;
	this.icon = icon;
	
	//create task button div obj
	//property
	this.buttonObj = document.createElement("div");
	this.buttonObj.id = this.group.gid + "." + id;
	this.buttonObj.className = "taskmenuitem_div";
	this.buttonObj.innerHTML = this.name;
	
	
	//event	
	//click
	var oThis = this;
	this.buttonObj.onclick = function () {
		if (oThis.window) {
			oThis.window.doRestore();
			oThis.window.doActive();
		}
		oThis.group.hideTaskMenu();
	}
	
	//hover
	this.buttonObj.onmouseover = function () {
		oThis.buttonObj.className = "taskmenuitem_div_hover";
	}
	this.buttonObj.onmouseout = function () {
		oThis.buttonObj.className = "taskmenuitem_div";
	}
	
}

